package com.logistics.Action;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.DAO.Action;
import com.DAO.Daily;
import com.DAO.User;
import com.opensymphony.xwork2.ActionSupport;

public class Displayer extends ActionSupport implements ServletRequestAware{

	private static final long serialVersionUID = 1L;
	/**
	 * @title Manager.java
	 * @package com.logistics.Action
	 * @author ufo
	 */

	private Map<String,String> result=new HashMap<String,String>();
	private HttpServletRequest request;
	private String[] id;
	private String[] group;
	private String[] topStartDate;
	private String[] bottomStartDate;
	private String[] topCheckDate;
	private String[] bottomCheckDate;
	private String[] topEndDate;
	private String[] bottomEndDate;
	private String[] first;
	private String[] max;

	protected Configuration conf=new Configuration().configure();
	protected SessionFactory sf=conf.buildSessionFactory();
	protected Session hibernateSess=sf.openSession();
	protected Transaction tx = hibernateSess.beginTransaction();
	protected User user;
	protected Action action;

	public String display(){
		if(isOnline()){
			addSessionToUser();
			StringBuffer quary=new StringBuffer().append("from action where");
			if(!getId()[0].isEmpty())
				quary.append(" id="+getId()[0]);
			
			if((!getTopStartDate()[0].isEmpty())&&(!getBottomStartDate()[0].isEmpty())){
				if(!quary.equals("from action where"))
					quary.append(" and");
				quary.append(" startDate<='"+getTopStartDate()[0]+"' and startDate>='"+getBottomStartDate()[0]+"'");
			}
			if((!getTopCheckDate()[0].isEmpty())&&(!getBottomCheckDate()[0].isEmpty())){
				if(!quary.equals("from action where"))
					quary.append(" and");
				quary.append(" checkDate<='"+getTopCheckDate()[0]+"' and checkDate>='"+getBottomCheckDate()[0]+"'");
			}
			
			if((!getTopEndDate()[0].isEmpty())&&(!getBottomEndDate()[0].isEmpty())){
				if(!quary.equals("from daily where"))
					quary.append(" and");
				quary.append(" endDate<='"+getTopEndDate()[0]+"' and endDate>='"+getBottomEndDate()[0]+"'");
			}
			if(!getGroup()[0].isEmpty()){
				if(!quary.equals("from daily where"))
					quary.append(" and");
				quary.append(" group="+getGroup()[0]);
			}
			if(user.getFlag()==1){
				if(!quary.equals("from daily where"))
					quary.append(" and");
				quary.append(" tid="+request.getSession().getAttribute("userId"));
			}else{
				result.put("flag", "false");
				return SUCCESS;
			}
			List list=hibernateSess.createQuery(quary.toString())
					.setFirstResult(Integer.parseInt(getFirst()[0]))
					.setMaxResults(Integer.parseInt(getMax()[0]))
					.list();
			Iterator i=list.iterator();
			int a=0;
			for(;i.hasNext();a++){
				action=(Action) i.next();

				result.put("actionId:"+a, action.getId());
				result.put("actionTid:"+a,action.getTid());
				result.put("actionDescription:"+a,action.getDescription());
				result.put("actionGroup:"+a, action.getGroup());
				result.put("actionStartDate:"+a, action.getStartDate());
				result.put("actionCheckDate:"+a, action.getCheckDate());
				result.put("actionEndDate:"+a, action.getEndDate());
			}
			if(a>0)return SUCCESS;
		}
		result.put("flag", "false");
		return SUCCESS;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO 自动生成的方法存根
		this.request=request;
	}
	@JSON(name="resultt")
	public Map<String,String> getResult() {
		return result;
	}
	public void setResult(Map<String,String> result) {
		this.result = result;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	private void addSessionToUser(){
		user=(User) hibernateSess.load(User.class, (Serializable) request.getSession().getAttribute("userId"));
	}
	public boolean isOnline(){
		if(request.getSession().getAttribute("userId")!=null)
			return true;
		else 
			return false;
	}
	public String[] getGroup() {
		return group;
	}
	public void setGroup(String[] group) {
		this.group = group;
	}
	public String[] getTopStartDate() {
		return topStartDate;
	}
	public void setTopStartDate(String[] topStartDate) {
		this.topStartDate = topStartDate;
	}
	public String[] getBottomStartDate() {
		return bottomStartDate;
	}
	public void setBottomStartDate(String[] bottomStartDate) {
		this.bottomStartDate = bottomStartDate;
	}
	public String[] getTopCheckDate() {
		return topCheckDate;
	}
	public void setTopCheckDate(String[] topCheckDate) {
		this.topCheckDate = topCheckDate;
	}
	public String[] getBottomCheckDate() {
		return bottomCheckDate;
	}
	public void setBottomCheckDate(String[] bottomCheckDate) {
		this.bottomCheckDate = bottomCheckDate;
	}
	public String[] getTopEndDate() {
		return topEndDate;
	}
	public void setTopEndDate(String[] topEndDate) {
		this.topEndDate = topEndDate;
	}
	public String[] getBottomEndDate() {
		return bottomEndDate;
	}
	public void setBottomEndDate(String[] bottomEndDate) {
		this.bottomEndDate = bottomEndDate;
	}
	public String[] getId() {
		return id;
	}
	public void setId(String[] id) {
		this.id = id;
	}
	public String[] getFirst() {
		return first;
	}
	public void setFirst(String[] first) {
		this.first = first;
	}
	public String[] getMax() {
		return max;
	}
	public void setMax(String[] max) {
		this.max = max;
	}
}